/*
* $Id: ViewPoints.cpp 153 2010-09-26 17:33:07Z volok-aleksej@yandex.ru $
*
* Copyright 2009 by Volokitin Alexey
* All rights reserved
*
*/
#include "Common/include.h"
#include "ViewPoints.h"
#include "Components/Journal/FamilyManager.h"
#include "Settings/Journal/PointViewSettings.h"
#include "Settings/DinIntDefined.h"
#include "Settings/SettingControlIds.h"
#include "ViewFieldValuesHelper.h"
#include "Event/EventId.h"

#pragma warning(disable: 4407)

CViewPoints::CViewPoints(CState* pState)
: CViewTable(pState){
	IEventManager::GetEventManager()->SubscribeEvent(this, POINTS_DB_CHANGE_EVENT);
	IEventManager::GetEventManager()->SubscribeEvent(this, POINT_STN_CHANGE_EVENT);
	Init();
}
CViewPoints::~CViewPoints(){
	IEventManager::GetEventManager()->UnSubscribeEvent(this, POINTS_DB_CHANGE_EVENT);
	IEventManager::GetEventManager()->UnSubscribeEvent(this, POINT_STN_CHANGE_EVENT);
}

bool CViewPoints::IsViewState(State state){
	return state.m_nPosTab == Journal::INSPECT_JOURNAL &&
		state.m_type == Journal::NONE &&
		state.m_nTypeSel == Journal::NOTSEL;
}

void CViewPoints::SetState(){
	m_pState->m_nState = Journal::VIEW_POINT;
	m_pState->m_nIdParameter = 0;
}
void CViewPoints::Init(){
	m_tableRegion.GetTableDrawer()->ResetTable();
	tableHeader *nameHeader = NULL, *noteHeader = NULL;
	nameHeader = m_tableRegion.GetTableDrawer()->AddHeader(ILanguageManager::GetLanguageManager()->GetString(TABLE_HEADER_POINT_NAME_ID), TABLE_POINT_NAME_ID, TypeField::STRING);
	CPointViewSettings pSettings;
	if(pSettings.GetState(CHECK_NOTE_ID) == CHECK_BOX_STATE_1)
		noteHeader = m_tableRegion.GetTableDrawer()->AddHeader(ILanguageManager::GetLanguageManager()->GetString(TABLE_HEADER_NOTE_ID), TABLE_POINT_NOTE_ID, TypeField::STRING);
	CViewFieldValuesHelper helper(m_tableRegion.GetTableDrawer());
	helper.GenerateUserHeaders(TypeField::POINTS, TABLE_POINT_USER_ID);
	m_tableRegion.GetTableDrawer()->NormalizeHeader();
	CFamilyManager* pManager = IJournalComponent::GetFamilyManager();
	std::map<int, CFamilyManager::point> aPoints = pManager->GetPoints();
	for(std::map<int, CFamilyManager::point>::iterator it = aPoints.begin(); it != aPoints.end(); it++){
		CTableCelRow *row = new CTableCelRow(it->first, Journal::VIEW_POINT);
		row->AddCel(nameHeader, it->second.m_strName);
		if(noteHeader)
			row->AddCel(noteHeader, it->second.m_strNote);
		helper.GenerateUserCels(row, it->second.m_nIdRecord);
		m_tableRegion.GetTableDrawer()->AddCelRow(row);
	}
}
void CViewPoints::SetManipulation(Journal::typeItemMnpl type, int nId){
	IWnd* pWnd = IVisualManager::GetVisualManager()->
		GetVisualComponent(ILanguageManager::GetLanguageManager()->GetString(JOURNAL_CAPTION_ID))->GetComponentFrame();
	pWnd->Post(UM_ITEM_MANIPULATION, type, nId);
}

BEGIN_MESSAGE_MAP(CViewPoints, CViewTable)
	ON_COMMAND(BUTTON_ADD_ID, OnAddPoint)
	ON_COMMAND(BUTTON_DELETE_ID, OnDeletePoint)
	ON_COMMAND(BUTTON_CHANGE_ID, OnChangePoint)
END_MESSAGE_MAP()

void CViewPoints::OnAddPoint(){
	SetManipulation(Journal::ADD_POINT, 0);
}
void CViewPoints::OnDeletePoint(){
	std::tstring strContent = ILanguageManager::GetLanguageManager()->GetString(QUESTION_DELETE_ID) +
		_T(" '") + m_tableRegion.GetTableDrawer()->GetSelectRow()->GetValueColumn(TABLE_POINT_NAME_ID) + _T("'.");
	if(MessageBox(strContent.c_str(), ILanguageManager::GetLanguageManager()->
		GetString(DELETE_POINT_ID).c_str(), MB_YESNO) == IDYES)
			SetManipulation(Journal::DELETE_POINT, m_tableRegion.GetTableDrawer()->GetSelectRow()->GetId());
}
void CViewPoints::OnChangePoint(){
	SetManipulation(Journal::CHANGE_POINT, m_tableRegion.GetTableDrawer()->GetSelectRow()->GetId());
}

void CViewPoints::EventHandler(){
	Init();
	if(IsCreate())
		Invalidate(FALSE);
}